package com.example.bootservice.util;

import org.springframework.boot.web.context.WebServerInitializedEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

import java.net.InetAddress;

@Component
public class ServerConfig implements ApplicationListener<WebServerInitializedEvent> {

    private int ServerPort;

    public String getUrl() {
        String adders =null;
        try {
            adders = InetAddress.getLocalHost().getHostAddress();
        }catch (Exception e){
            e.printStackTrace();
        }

        return  "http://" + adders + ":" + this.ServerPort;
    }
    @Override
    public void onApplicationEvent(WebServerInitializedEvent webServerInitializedEvent) {
        this.ServerPort = webServerInitializedEvent.getWebServer().getPort();
    }
}
